//
//  JRHttpRequestManager+Rx.swift
//  JRHttpRequestManager
//
//  Created by 京睿 on 2017/8/22.
//  Copyright © 2017年 JingRuiWangKe. All rights reserved.
//

import Foundation
import RxSwift

public enum JRError: Error {
    case normal(String)
    case notJson
}

public extension JRHttpRequestManager {    
    public class func rxRequest(_ parameter: [String: Any]) -> Observable<[String: Any]> {
        return Observable.create { observer in
            
            JRHttpRequestManager
                .init(POST: .common, params: parameter)
                .successful {
                    if let result = $0 {
                        observer.onNext(result)
                        observer.onCompleted()
                    } else {
                        observer.onError(JRError.notJson)
                    }
                }
                .failed {
                    observer.onError(JRError.normal($0 ?? ""))
            }
            
            return Disposables.create()
        }
    }
}
